package com.hua.word;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.HyperlinkTextRenderData;
import com.deepoove.poi.data.Pictures;
import com.deepoove.poi.data.TextRenderData;
import com.hua.utils.Result;
import org.springframework.stereotype.Component;

import javax.servlet.ServletOutputStream;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


@Component
public class wordTemp {

    public Result word(String path,String dir,String name){
        String dirPath =path+":\\"+dir+"\\";
        File file = new File(dirPath); //以某路径实例化一个File对象
        if (!file.exists()){ //如果不存在
            boolean dr = file.mkdirs(); //创建目录
        }
        // 模板文件输入输出地址
        String filePath = "./src/main/resources/static/p.docx";
        String outPath = dirPath+name+".docx";
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-dd");
        Map<String,Object> map = new HashMap<>();
        map.put("test00","这是标题");
        map.put("image","./src/main/java/com/hua/word/logo.png");
//        map.put("image", Pictures.ofLocal("./src/main/java/com/hua/word/logo.png").size(120, 120).create());
        map.put("author", new TextRenderData("000000", "Sayi"));
        map.put("link", new HyperlinkTextRenderData("website", "http://deepoove.com"));
        map.put("anchor", new HyperlinkTextRenderData("anchortxt", "anchor:appendix1"));
        XWPFTemplate template = XWPFTemplate.compile(filePath).render(map);
        try {
//            ServletOutputStream out = new ServletOutputStream();
            FileOutputStream out = new FileOutputStream(outPath);
            template.writeAndClose(out);
            System.out.println(out);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Result.ok();
    }
}
